home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / OBJC257.ZIP / include / objc / list.h < prev    next >
C/C++ Source or Header  |  1993-12-15  |  3KB  |  146 lines

  1. /* Generic single linked list to keep various information 
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. Author: Kresten Krab Thorup
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify it under the
  9.    terms of the GNU General Public License as published by the Free Software
  10.    Foundation; either version 2, or (at your option) any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
  13.    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14.    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  15.    details.
  16.  
  17. You should have received a copy of the GNU General Public License along with
  18.    GNU CC; see the file COPYING.  If not, write to the Free Software
  19.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* As a special exception, if you link this library with files compiled with
  22.    GCC to produce an executable, this does not cause the resulting executable
  23.    to be covered by the GNU General Public License. This exception does not
  24.    however invalidate any other reasons why the executable file might be
  25.    covered by the GNU General Public License.  */
  26.  
  27. void * __objc_xrealloc (void *optr, size_t size);
  28. void * __objc_xmalloc (size_t size);
  29.  
  30. struct objc_list {
  31.   void *head;
  32.   struct objc_list *tail;
  33. };
  34.  
  35. /* Return a cons cell produced from (head . tail) */
  36.  
  37. static inline struct objc_list* 
  38. list_cons(void* head, struct objc_list* tail)
  39. {
  40.   struct objc_list* cell;
  41.  
  42.   cell = (struct objc_list*)__objc_xmalloc(sizeof(struct objc_list));
  43.   cell->head = head;
  44.   cell->tail = tail;
  45.   return cell;
  46. }
  47.  
  48. /* Return the length of a list, list_length(NULL) returns zero */
  49.  
  50. static inline int
  51. list_length(struct objc_list* list)
  52. {
  53.   int i = 0;
  54.   while(list)
  55.     {
  56.       i += 1;
  57.       list = list->tail;
  58.     }
  59.   return i;
  60. }
  61.  
  62. /* Return the Nth element of LIST, where N count from zero.  If N 
  63.    larger than the list length, NULL is returned  */
  64.  
  65. static inline void*
  66. list_nth(int index, struct objc_list* list)
  67. {
  68.   while(index-- != 0)
  69.     {
  70.       if(list->tail)
  71.     list = list->tail;
  72.       else
  73.     return 0;
  74.     }
  75.   return list->head;
  76. }
  77.  
  78. /* Remove the element at the head by replacing it by its successor */
  79.  
  80. static inline void
  81. list_remove_head(struct objc_list** list)
  82. {
  83.   if ((*list)->tail)
  84.     {
  85.       struct objc_list* tail = (*list)->tail; /* fetch next */
  86.       *(*list) = *tail;        /* copy next to list head */
  87.       free(tail);            /* free next */
  88.     }
  89.   else                /* only one element in list */
  90.     {
  91.       free (*list);
  92.       (*list) = 0;
  93.     }
  94. }
  95.  
  96.  
  97. /* Remove the element with `car' set to ELEMENT */
  98.  
  99. static inline void
  100. list_remove_elem(struct objc_list** list, void* elem)
  101. {
  102.   while (*list) {
  103.     if ((*list)->head == elem)
  104.       list_remove_head(list);
  105.     list = &((*list)->tail);
  106.   }
  107. }
  108.  
  109. /* Map FUNCTION over all elements in LIST */
  110.  
  111. static inline void
  112. list_mapcar(struct objc_list* list, void(*function)(void*))
  113. {
  114.   while(list)
  115.     {
  116.       (*function)(list->head);
  117.       list = list->tail;
  118.     }
  119. }
  120.  
  121. /* Return element that has ELEM as car */
  122.  
  123. static inline struct objc_list**
  124. list_find(struct objc_list** list, void* elem)
  125. {
  126.   while(*list)
  127.     {
  128.     if ((*list)->head == elem)
  129.       return list;
  130.     list = &((*list)->tail);
  131.     }
  132.   return NULL;
  133. }
  134.  
  135. /* Free list (backwards recursive) */
  136.  
  137. static void
  138. list_free(struct objc_list* list)
  139. {
  140.   if(list)
  141.     {
  142.       list_free(list->tail);
  143.       free(list);
  144.     }
  145. }
  146.